import { getAjax, postAjax, putAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'
export interface DictData {
  dictLabel: string | null;
  dictValue: string | null;
  dictCode?: number | null;
  dictSort?: number | null;
  dictType?: string | null;
  cssClass?: string | null;
  listClass?: string | null;
  isDefault?: boolean | null;
  enable?: boolean | null;
  remark?: string | null;
}

export interface StateType {
  data: DictData;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_DATA: Mutation<StateType>;
  };
  actions: {
    getList: Action<StateType, StateType>;
    getDictData: Action<StateType, StateType>;
    addDictData: Action<StateType, StateType>;
    updateDictData: Action<StateType, StateType>;
    deleteDictData: Action<StateType, StateType>;
    getDicts: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      dictLabel: '',
      dictValue: ''
    }
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data = data
    }
  },
  actions: {
    getList(_, data) {
      return getAjax('/system/dict/data/list', data)
    },
    getDictData(_, id) {
      return getAjax(`/system/dict/data/${id}`, null)
    },
    addDictData(_, data) {
      return postAjax('/system/dict/data', data)
    },
    updateDictData(_, data) {
      return putAjax('/system/dict/data', data)
    },
    deleteDictData(_, id) {
      return deleteAjax(`/system/dict/data/${id}`, null)
    },
    getDicts(_, dictType) {
      return getAjax(`/system/dict/data/type/${dictType}`, null)
    }
  }
}

export default storeModel
